// 素材类型定义，使用常量对象替代枚举，增强灵活性和类型安全性。
// 每个素材类型都有唯一的键值对，便于扩展和维护。
export const MaterialType = {
  TEXT: "TEXT", // 文本
  IMAGE: "IMAGE", // 图片
  VIDEO: "VIDEO", // 视频
  AUDIO: "AUDIO", // 音频
  PTT: "PTT", // 语音
  FILE: "FILE", // 文件
  STICKER: "STICKER", // 表情包
  GIF: "GIF", // 动图
  CARD: "CARD", // 名片
  LINK: "LINK", // 链接
  MERCHANT_LINK: "MERCHANT_LINK", // 商户链接（转发超链）
  AVATAR: "AVATAR", // 头像
  USERNAME: "USERNAME", // 用户名
  NICKNAME: "NICKNAME", // 昵称
  SIGNATURE: "SIGNATURE", // 签名
  WEBSITE: "WEBSITE", // 个人网站
  LOCATION: "LOCATION", // 位置
  IMAGETEXTTEMPLATE: "IMAGETEXTTEMPLATE", // 图文模板
  POLL: "POLL", // 投票
} as const;

// 定义类型
export type MaterialTypeKeys = keyof typeof MaterialType;
export type MaterialTypeValues = (typeof MaterialType)[MaterialTypeKeys];

// 映射表
const baseMaterialTypeDescriptions: Record<MaterialTypeKeys, string> = {
  AVATAR: "1",
  USERNAME: "2",
  NICKNAME: "3",
  SIGNATURE: "4",
  WEBSITE: "5",
  TEXT: "6",
  IMAGE: "7",
  VIDEO: "8",
  AUDIO: "9",
  PTT: "10",
  FILE: "11",
  STICKER: "12",
  GIF: "13",
  CARD: "14",
  LINK: "15",
  MERCHANT_LINK: "16",
  LOCATION: "17",
  IMAGETEXTTEMPLATE: "18",
  POLL: "19",
};

// 为了避免重复的字符串字面量类型检查，这里直接使用类型断言
export const MaterialTypeDescriptions: Record<MaterialTypeKeys, string> =
  baseMaterialTypeDescriptions;

// 根据 materialType 转成对应的映射值
export const MaterialTypeToMapping = (
  materialType: MaterialTypeKeys
): string => {
  return MaterialTypeDescriptions[materialType];
};

// 消息类型枚举
export const MessageType = {
  TEXT: "TEXT",
  IMAGE: "IMAGE",
  VIDEO: "VIDEO",
  AUDIO: "AUDIO",
  PTT: "PTT",
  DOCUMENT: "DOCUMENT",
  STICKER: "STICKER",
  GIF: "GIF",
  CARD: "CARD",
  URL: "URL",
  MERCHANT_URL: "MERCHANT_URL",
  IMAGETEXTTEMPLATE: "IMAGETEXTTEMPLATE",
  POLL: "POLL",
  VOICE_CALL: "VOICE_CALL",
  VIDEO_CALL: "VIDEO_CALL",
  PTV: "PTV",
  EXTENDED_TEXT: "EXTENDED_TEXT",
  UNKNOWN: "UNKNOWN",
} as const;

export type MessageTypeKeys = keyof typeof MessageType;
export type MessageTypeValues = (typeof MessageType)[MessageTypeKeys];

// materialType 转 MessageType
export const MaterialTypeToMessageType = (
  materialType: MaterialTypeKeys
): MessageTypeValues => {
  // 使用对象映射替代 switch 语句
  const mapping: { [key in MaterialTypeKeys]: MessageTypeValues } = {
    AVATAR: MessageType.IMAGE,
    USERNAME: MessageType.TEXT,
    NICKNAME: MessageType.TEXT,
    SIGNATURE: MessageType.TEXT,
    WEBSITE: MessageType.TEXT,
    TEXT: MessageType.TEXT,
    IMAGE: MessageType.IMAGE,
    VIDEO: MessageType.VIDEO,
    AUDIO: MessageType.AUDIO,
    PTT: MessageType.PTT,
    FILE: MessageType.DOCUMENT,
    STICKER: MessageType.STICKER,
    GIF: MessageType.GIF,
    CARD: MessageType.CARD,
    LINK: MessageType.URL,
    MERCHANT_LINK: MessageType.MERCHANT_URL,
    LOCATION: MessageType.TEXT,
    IMAGETEXTTEMPLATE: MessageType.IMAGETEXTTEMPLATE,
    POLL: MessageType.POLL, // 投票
  };

  // 提取公共的检查和返回逻辑
  const result = mapping[materialType];
  return result === undefined ? MessageType.TEXT : result;
};

// 素材 映射值转 MessageType
export const MaterialMappingToMessageType = (
  materialMapping: string
): MessageTypeValues => {
  // 使用对象映射替代 switch 语句
  const mapping: { [key: string]: MessageTypeValues } = {
    "1": MessageType.IMAGE,
    "2": MessageType.TEXT,
    "3": MessageType.TEXT,
    "4": MessageType.TEXT,
    "5": MessageType.TEXT,
    "6": MessageType.TEXT,
    "7": MessageType.IMAGE,
    "8": MessageType.VIDEO,
    "9": MessageType.AUDIO,
    "10": MessageType.PTT,
    "11": MessageType.DOCUMENT,
    "12": MessageType.STICKER,
    "13": MessageType.GIF,
    "14": MessageType.CARD,
    "15": MessageType.URL,
    "16": MessageType.MERCHANT_URL,
    "17": MessageType.TEXT,
    "18": MessageType.IMAGETEXTTEMPLATE,
    "19": MessageType.POLL,
  };

  // 提取公共的检查和返回逻辑
  const result = mapping[materialMapping];
  return result === undefined ? MessageType.TEXT : result;
};

// 名片信息接口
export interface CardInfo {
  // 显示名称
  displayName: string;
  // 名
  firstName: string;
  // 联系方式
  telNumber: string;
  // 姓
  lastName?: string;
}

// 图文链接
export interface LinkInfo {
  // 文本
  text: string;
  // 链接描述
  description: string;
  // 缩略图
  jpegThumbnail: string;
  // 链接标题
  title: string;
  // 附加文本
  matchedText: string;
}

// 图文模版
export interface LinkTemplateInfo {
  imageKey: string;
  imageName: string;
  content: string;
  displayText: string;
  url?: string;
  urls?: string[];
  imageUrl?: string;
}

// 转发超链
export interface ForwardLinkInfo {
  // 链接体
  body: string;
  // 链接标题
  title: string;
  // 好友的号码
  businessOwnerRawJid: string;
}

// 定位
export interface LocationInfo {
  // 纬度
  latitude: number;
  // 经度
  longitude: number;
  // 位置名称
  name: string;
  // 缩略图
  jpegThumbnail: string;
  // 位置Url
  url: string;
}

// 投票
export interface PollInfo {
  // 投票主题
  question: string;
  // 投票选项
  options: string[];
  // 可选择的数量
  selectableOptionCount: number;
}

// 消息项接口
export interface MessageItem {
  messageType: MessageTypeValues;
  messageContent?: string;
  messageContentUrl?: string;
  messageFileName?: string;
  fromType?: number; // 1 会话 2素材库  undefined
}

// 基础分页查询接口
export interface PageQuery {
  page?: number;
  pageSize?: number;
  isPublic?: boolean;
}

// ========== MaterialSelector 组件相关类型 ==========

// 素材项接口
export interface MaterialItem {
  id: string;
  content: string;
  showActions?: boolean;
  name?: string;
  remark?: string;
  fileKey?: string;
  type?: string;
}

// 文件夹项接口
export interface FolderItem {
  id: string;
  name: string;
  showActions?: boolean;
  items: MaterialItem[];
}

// 素材标签页接口
export interface MaterialTab {
  label: string;
  type: string;
  accept: string;
  display?: boolean;
}

// 素材 API 接口
export interface MaterialAPI {
  listMaterialGroup: (params: {
    page: number;
    pageSize: number;
    isPublic: boolean;
    type: string;
    name?: string;
    useTypes: number[];
  }) => Promise<{ materialGroups: any[] }>;

  listMaterial: (params: {
    groupId: string;
    page: number;
    pageSize: number;
    name?: string;
    useTypes: number[];
  }) => Promise<{ materials: any[] }>;

  createMaterialGroup: (params: {
    name: string;
    type: string;
    isPublic: boolean;
    useType: number;
  }) => Promise<any>;

  updateMaterialGroup: (params: {
    id: string;
    name: string;
    isPublic: boolean;
  }) => Promise<any>;

  deleteMaterialGroup: (params: { id: string }) => Promise<any>;

  createMaterial: (params: {
    name: string;
    content: string;
    groupId: string;
    type: string;
    useType: number;
  }) => Promise<any>;

  updateMaterial: (params: {
    id: string;
    content?: string;
    remark?: string;
  }) => Promise<any>;

  batchDeleteMaterial: (params: { ids: string[] }) => Promise<any>;
}

// 文件上传处理器
export type UploadHandler = (
  file: File
) => Promise<{ key: string; url: string }>;

// MaterialSelector 组件 Props
export interface MaterialSelectorProps {
  visible: boolean;
  title?: string;
  dialogWidth?: string;
  tabs: MaterialTab[];
  api?: MaterialAPI;
  uploadHandler?: UploadHandler;
  t?: (key: string) => string;
  uploadLimit?: number;
  maxFileSize?: number;
  enablePublicPrivate?: boolean;
}
